package game;

import game.connection.GameInitiate;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JComponent;

/**
 * Draw the field by Graphic2D.
 * 
 * @author Sarun Wongtanakarn 5510546166
 * @author Apiwat Jierakulpanit 5510546212
 * @Version 2013.05.01
 */
public class GameDraw extends JComponent {

	private GameInitiate game;
	private Field field;
	private Map<Integer, Integer> healthData;

	/**
	 * Creates the GameDraw with giving GameInitiate.
	 * @param game the GameInitiate
	 */
	public GameDraw(GameInitiate game) {
		this.game = game;
		healthData = new HashMap<Integer, Integer>();
		setField(game.FIELD);
	}

	/**
	 * Sets the field of game.
	 * @param field the field for setting
	 */
	public void setField(Field field) {
		this.field = field;
	}

	/**
	 * Sets the health of each players.
	 * @param healthData the map of health data
	 */
	public void setHealth(Map<Integer, Integer> healthData) {
		this.healthData = healthData;
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;

		drawBackGround(g2, new Color(158, 158, 158));
		drawFieldBackground(g2, Color.BLACK);

		for (int y = field.getHeight() - 1; y >= 0; y--) {
			for (int x = 0; x < field.getWidth(); x++) {
				int value = field.getValue(field.POINT[x][y]);
				if (value == 0)
					continue;
				Rectangle2D box = new Rectangle2D.Double(x * 10,
						(field.getHeight() - y) * 10, 10, 10);
				Color color = ColorMap.get(value);

				g2.setColor(color);
				g2.draw(box);
				g2.setPaint(color);
				g2.fill(box);
			}

		}

		drawHealthBar(g2);
		drawMoney(g2);
	}

	/**
	 * Draws the bar of current money.
	 * @param g2 the Graphics2D for setting
	 */
	private void drawMoney(Graphics2D g2) {
		int x = game.FIELD.getWidth() * 11;
		int y = 30;

		int w = 100;
		int h = 20;

		Font font = new Font("Arial", Font.BOLD, 22);
		g2.setFont(font);
		g2.setStroke(new BasicStroke(2.0F));
		g2.setPaint(new Color(0, 255, 0));
		Rectangle2D background = new Rectangle2D.Double(x - 10, y - 20, 235, 30);
		g2.fill(background);
		g2.setPaint(Color.BLACK);
		g2.drawString("$: " + game.PLAYER.getMoney(), x, y);

	}

	/**
	 * Draws the pixels on the field.
	 * @param g2 the Graphics2D for setting
	 * @param color the color of pixel
	 */
	private void drawBackGround(Graphics2D g2, Color color) {
		Rectangle r = g2.getClipBounds();
		int w = (int) r.getWidth();
		int h = (int) r.getHeight();
		Rectangle2D box = new Rectangle2D.Double(0, 0, w, h);
		g2.setPaint(color);
		g2.fill(box);
	}

	/**
	 * Draws the pixels on the background.
	 * @param g2 the Graphics2D for setting
	 * @param color the color of pixel
	 */
	private void drawFieldBackground(Graphics2D g2, Color color) {
		Rectangle2D box = new Rectangle2D.Double(0, 0, field.getWidth() * 10,
				field.getHeight() * 10 + 10);
		g2.setPaint(color);
		g2.fill(box);
		g2.setColor(Color.RED);

	}

	/**
	 * Draws the health bars.
	 * @param g2 the Graphics2D for setting
	 */
	private void drawHealthBar(Graphics2D g2) {
		int x = game.FIELD.getWidth() * 11;
		int y = 60;

		int w = 100;
		int h = 20;

		g2.setPaint(new Color(68, 68, 68));
		Rectangle2D background = new Rectangle2D.Double(x - 10, y - 10, 235,
				240);
		g2.fill(background);

		for (int i = 1; i <= game.MAXIMUM_PLAYER; i++) {

			Font font = new Font("Arial", Font.BOLD, 22);
			g2.setFont(font);
			g2.setStroke(new BasicStroke(2.0F));

			g2.setColor(ColorMap.get(i));
			g2.setPaint(ColorMap.get(i));
			g2.drawString(game.PLAYER_NAMES.get(i), x, y + 25 * i);

			Rectangle2D health = new Rectangle2D.Double(x + 100, y + 25 * i
					- 15, w, h);
			g2.draw(health);

			try {
				Rectangle2D health2 = new Rectangle2D.Double(x + 100, y + 25
						* i - 15, 100 * healthData.get(i)
						/ game.PLAYER.MAXIMUM_HEALTH, h);
				g2.fill(health2);

			} catch (Exception ex) {

			}

			Font font2 = new Font("Arial", Font.BOLD, 12);
			g2.setFont(font2);
			g2.setStroke(new BasicStroke(1.0F));
			g2.setColor(Color.BLACK);
			g2.drawString(healthData.get(i) + "/" + game.PLAYER.MAXIMUM_HEALTH,
					x + 130, y + 27 * i - 5);

		}

	}
}
